import {
  getWaresList,
  getBrandsWaresList,
  getBrandsList,
  getWaresDetail,
  getStyleCostDetail,
  addCart,
  getCartList,
  updateCartNumber,
  deleteCart
} from '../../request/api'
Page({
  data: {
    cartShow: false,
    checked: false,
    checkedAll: false,
    detailShow: false,
    waresList: [],
    waresDetail: {},
    isActive: 0,
    styleCost: [],
    images: '',
    isCostActive: 0,
    loginShow: false,
    token: wx.getStorageSync('token'),
    waresNumber: 1,
    cartList: {},
    editShow: true,
    totalPrice: 0,
    disabled: false,
    activeName: '1',
    isBrrandsActive: 0,
    demoArr: [{
      brandsName: '全部商品'
    }],
    brandsList: [],
    brandsIndex: null,
    addCartLoading: false
  },
  // 点击展示购物车弹出层
  onClickIcon() {
    if (wx.getStorageSync('token')) {
      this.setData({
        cartShow: true
      })
    } else {
      this.setData({
        loginShow: true
      })
    }
  },
  // 点击关闭购物车弹出层
  onClose() {
    this.setData({
      cartShow: false
    })
  },
  // 点击展开商品详情弹出层
  async showDetail(e) {
    this.getCartListData()
    let that = this
    this.setData({
      detailShow: true
    })
    let waresId = e.currentTarget.dataset.waresid
    const res = await getWaresDetail(waresId)
    console.log('aa', res)
    if (res.data.code == 200) {
      res.data.name = e.currentTarget.dataset.name
      console.log('商品详情', res)
      that.setData({
        waresDetail: res.data
      })

      this.data.waresDetail.waresId = waresId

      this.getStyleCostDetailData(res.data.data[this.data.isActive].id)
    }
  },
  // 点击关闭商品详情弹出层
  detailClose() {
    this.setData({
      detailShow: false,
      isActive: 0,
      isCostActive: 0
    })
  },
  addBrandsClass(e) {
    let brandsId = e.currentTarget.dataset.brandsid
    let index = e.currentTarget.dataset.index
    this.setData({
      isBrrandsActive: e.currentTarget.dataset.index
    })
    if (index == 0) {
      this.getAllWaresListData()
    } else {
      this.getWaresListData(brandsId)
    }
  },
  // 获取全部商品
  async getAllWaresListData() {
    const res = await getWaresList()
    if (res.data.code == 200) {
      console.log('全部商品', res.data.data)
      this.setData({
        waresList: res.data.data
      })
    }
  },

  // 获取商品列表
  async getWaresListData(brandsId) {
    const res = await getBrandsWaresList(brandsId)
    if (res.data.code == 200) {
      console.log('商品列表', res)
      this.setData({
        waresList: res.data.data
      })
    }
  },
  // 获取分类列表
  async getBrandsListData() {
    const res = await getBrandsList()
    if (res.data.code == 200) {
      console.log('分类列表', res)
      let arr = res.data.data
      let demo = this.data.demoArr.concat(arr)
      this.setData({
        brandsList: demo
      })
    }
  },
  // 点击切换红包款式
  itemClick(e) {
    console.log(e)
    let index = e.currentTarget.dataset.index
    this.setData({
      isActive: index
    })
    let styleId = e.currentTarget.dataset.id
    this.getStyleCostDetailData(styleId)
  },
  // 当点击切换了红包款式之后，展示当前的套餐
  async getStyleCostDetailData(styleId) {
    const res = await getStyleCostDetail(styleId)
    if (res.data.code == 200) {
      console.log('红包套餐', res)
      this.setData({
        styleCost: res.data.data.list,
        images: res.data.data.images
      })
    }

  },
  // 点击切换红包套餐
  costClick(e) {
    this.setData({
      isCostActive: e.currentTarget.dataset.index
    })
  },


  // 加入购物车
  async addCartData() {
    if (!wx.getStorageSync('token')) {
      wx.showModal({
        title: '提示',
        content: '您还未进行登录，是否前往登录？',
        complete: (res) => {
          if (res.cancel) {
          }
          if (res.confirm) {
            wx.switchTab({
              url: '/pages/user/user'
            })
          }
        }
      })
      return
    }
    this.setData({
      addCartLoading: true
    })
    let sku = {
      waresId: this.data.waresDetail.waresId,
      styleId: this.data.waresDetail.data[this.data.isActive].id,
      costId: this.data.styleCost[this.data.isCostActive].id,
      waresNumber: this.data.waresNumber
    }
    console.log(JSON.stringify(sku));
    const res = await addCart(JSON.stringify(sku));
    if (res.data.code == 200) {
      wx.showToast({
        title: '加入成功',
        icon: 'success'
      })
      console.log('加入购物车', res);
      this.setData({
        detailShow: false,
        addCartLoading: false
      })
    }
  },

  // 购物车列表
  async getCartListData() {
    const res = await getCartList()
    if (res.data.code == 200) {
      let a = 0
      res.data.data.forEach((v) => {
        a += v.costPrice * v.waresNumber
      })
      res.data.totalPrice = a.toFixed(2)
      let b = 0
      res.data.data.forEach((item) => {
        b += item.waresNumber
      })
      res.data.totalNumber = b
      console.log('购物车列表', res);
      this.setData({
        cartList: res.data
      })
      res.data.data.forEach((v) => {
        v.checked = this.data.checked
      })
    }

  },

  // 更新购物车数量
  async updateCartNumberData(event) {
    if (event.currentTarget.dataset.num == "-1") {
      let obj1 = {
        newNumber: -1,
        cartId: event.currentTarget.dataset.id
      }
      console.log('减少的obj1', obj1);
      const res = await updateCartNumber(JSON.stringify(obj1))
      if (res.data.code == 200) {
        console.log('减少数量', res)
        this.getCartListData()
      }
    }
    if (event.currentTarget.dataset.num == "1") {
      let obj2 = {
        newNumber: 1,
        cartId: event.currentTarget.dataset.id
      }
      console.log('添加的obj2', obj2);
      const res = await updateCartNumber(JSON.stringify(obj2))
      if (res.data.code == 200) {
        console.log('添加数量', res)
        this.getCartListData()
      }
    }
  },

  // 向左滑动进行单个购物车删除
  async deleteOneCartData(e) {
    let data = {
      cartId: e.currentTarget.dataset.id,
      allOrOne: "one"
    }
    const res = await deleteCart(JSON.stringify(data))
    if (res.data.code == 200) {
      console.log('删除单个购物车', res);
      wx.showToast({
        title: '删除成功',
        icon: 'success'
      })
      this.getCartListData()
    }
  },

  // 点击全部删除
  async deleteAllCartData() {
    let data = {
      allOrOne: "all"
    }
    const res = await deleteCart(JSON.stringify(data))
    if (res.data.code == 200) {
      console.log('清空购物车', res);
      wx.showToast({
        title: '清空成功',
        icon: 'success'
      })
      this.getCartListData()
    }
  },

  // 去订单页进行结算
  goToOrders() {
    wx.navigateTo({
      url: '/pages/create-order/create-order',
    })
    this.setData({
      cartShow: false
    })
  },
  onClickButton() {
    console.log(this.data.cartList.data);
    if (this.data.cartList.data.length < 1) {
      wx.showToast({
        title: '您的购物车中还没有商品',
        icon: 'none'
      })
    } else {
      wx.navigateTo({
        url: '/pages/create-order/create-order',
      })
    }

  },

  onLoad() {
    this.getBrandsListData()
    this.getAllWaresListData()
  },
})